<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = [
        'customer_id', 'sku', 'name', 'unit', 'condition', 'validity', 'lockup_validity',
    ];

    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope('customer', function (Builder $builder) {
            if (request()->has('customer_id')) {
                $builder->where('customer_id', '=', request()->input('customer_id'));
            }
        });
    }

    /**
     * 产品图片
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function images()
    {
        return $this->hasMany(ProductImage::class);
    }

    /**
     * 产品库存信息
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @author luffyzhao@vip.126.com
     */
    public function inventory()
    {
        return $this->hasOne(ProductInventory::class);
    }

    /**
     * 产品自身属性
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @author luffyzhao@vip.126.com
     */
    public function volume()
    {
        return $this->hasOne(ProductVolume::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function logs(){
        return $this->hasMany(ProductLog::class);
    }

    /**
     * @return bool
     * @author luffyzhao@vip.126.com
     */
    public function isDraft(){
        return $this->getAttribute('status') === '0';
    }

    /**
     * @return bool
     * @author luffyzhao@vip.126.com
     */
    public function isNormal(){
        return $this->getAttribute('status') === '1';
    }

    /**
     * 没锁
     * @return bool
     * @author luffyzhao@vip.126.com
     */
    public function isUnlock(){
        return $this->getAttribute('status') === '1' && $this->getAttribute('lock_status') === '0';
    }

    /**
     * 锁定
     * @return bool
     * @author luffyzhao@vip.126.com
     */
    public function isLock(){
        return $this->getAttribute('status') === '1' && $this->getAttribute('lock_status') === '1';
    }
}
